<?php

namespace App\Traits;

trait HasStatus
{
    /**
     * 获取状态文本
     */
    public function getStatusTextAttribute(): string
    {
        $statusTexts = $this->getStatusTexts();
        return $statusTexts[$this->status] ?? $this->status;
    }

    /**
     * 获取状态文本映射
     */
    protected function getStatusTexts(): array
    {
        return [
            'active' => '正常',
            'inactive' => '禁用',
            'pending' => '待处理',
            'processing' => '处理中',
            'completed' => '已完成',
            'cancelled' => '已取消',
            'approved' => '已通过',
            'rejected' => '已拒绝',
        ];
    }

    /**
     * 检查是否为活跃状态
     */
    public function isActive(): bool
    {
        return $this->status === 'active';
    }

    /**
     * 检查是否为待处理状态
     */
    public function isPending(): bool
    {
        return $this->status === 'pending';
    }

    /**
     * 检查是否为处理中状态
     */
    public function isProcessing(): bool
    {
        return $this->status === 'processing';
    }

    /**
     * 检查是否为已完成状态
     */
    public function isCompleted(): bool
    {
        return $this->status === 'completed';
    }

    /**
     * 检查是否为已取消状态
     */
    public function isCancelled(): bool
    {
        return $this->status === 'cancelled';
    }

    /**
     * 检查是否为已通过状态
     */
    public function isApproved(): bool
    {
        return $this->status === 'approved';
    }

    /**
     * 检查是否为已拒绝状态
     */
    public function isRejected(): bool
    {
        return $this->status === 'rejected';
    }

    /**
     * 设置为活跃状态
     */
    public function markAsActive(): bool
    {
        return $this->update(['status' => 'active']);
    }

    /**
     * 设置为非活跃状态
     */
    public function markAsInactive(): bool
    {
        return $this->update(['status' => 'inactive']);
    }

    /**
     * 设置为待处理状态
     */
    public function markAsPending(): bool
    {
        return $this->update(['status' => 'pending']);
    }

    /**
     * 设置为处理中状态
     */
    public function markAsProcessing(): bool
    {
        return $this->update(['status' => 'processing']);
    }

    /**
     * 设置为已完成状态
     */
    public function markAsCompleted(): bool
    {
        return $this->update(['status' => 'completed']);
    }

    /**
     * 设置为已取消状态
     */
    public function markAsCancelled(): bool
    {
        return $this->update(['status' => 'cancelled']);
    }

    /**
     * 设置为已通过状态
     */
    public function markAsApproved(): bool
    {
        return $this->update(['status' => 'approved']);
    }

    /**
     * 设置为已拒绝状态
     */
    public function markAsRejected(): bool
    {
        return $this->update(['status' => 'rejected']);
    }

    /**
     * 状态作用域
     */
    public function scopeActive($query)
    {
        return $query->where('status', 'active');
    }

    public function scopeInactive($query)
    {
        return $query->where('status', 'inactive');
    }

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeProcessing($query)
    {
        return $query->where('status', 'processing');
    }

    public function scopeCompleted($query)
    {
        return $query->where('status', 'completed');
    }

    public function scopeCancelled($query)
    {
        return $query->where('status', 'cancelled');
    }

    public function scopeApproved($query)
    {
        return $query->where('status', 'approved');
    }

    public function scopeRejected($query)
    {
        return $query->where('status', 'rejected');
    }
}
